//
//  News.m
//  WY163News
//
//  Created by rong on 15/7/15.
//  Copyright (c) 2015年 rong. All rights reserved.
//

#import "News.h"
#import "NetWorkTools.h"

@implementation News

//根据 字典 返回 News对象模型
+(instancetype)newsWithDict:(NSDictionary *)dict {

    id obj = [[self alloc] init];
    
    [obj setValuesForKeysWithDictionary:dict];
    
    return obj;
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}


//打印对象的时候，自定义打印内容
-(NSString *)description {
    NSArray *properties = @[@"title", @"digest", @"replyCount", @"imgsrc"];
    NSDictionary *dict = [self dictionaryWithValuesForKeys:properties];
    
    return [NSString stringWithFormat:@"<%@: 父类:%@ %p> %@", self.class, self.superclass, self, dict];
}

//异步加载网络模型
+(void)loadNewsListWithURLString:(NSString *)urlString withFinished:(void (^)(NSArray *))finished {
    
    [[NetWorkTools sharedNetworkTools] GET:urlString parameters:nil success:^(NSURLSessionDataTask *task, NSDictionary* responseObject) {
        
        //NSLog(@"%@", responseObject);
        NSLog(@"======================");
        //先解析出来
        NSArray *dictArray = responseObject[responseObject.keyEnumerator.nextObject];
        //NSLog(@"%@", dictArray);
        
        NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:dictArray.count];
        
        for (id dict in dictArray) {
            News *new = [News newsWithDict:dict];
            [arrayM addObject:new];
        }
        
       // NSLog(@"%@", arrayM);
        
        //取得结果后，直接回调，因为是异步的，所以不能通过函数返回值返回，
        finished(arrayM.copy);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        //提示错误： NSLocalizedDescription=Request failed: unacceptable content-type: text/html
        //AFN框架默认不提供 “text/html” 格式的解析
        NSLog(@"%@", error);
    }];
}

@end
